package com.tjdr.his.drugstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjdr.his.domain.entity.StorageInfo;
import com.tjdr.his.domain.mapper.IStorageInfoMapper;
import com.tjdr.his.drugstore.service.IStorageInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @AUTHOR 张三
 * @DATE 2024/10/18 9:26
 * @DESCRIBER
 */
@Service
public class IStorageInfoServiceImpl implements IStorageInfoService {
    @Autowired
    private IStorageInfoMapper iStorageInfoMapper;

    @Override
    public IPage selectStorageInfoByProperty(String value,String drugType,int pageSize,int currentPage) {
        IPage<StorageInfo> page = new Page();
        int count =  iStorageInfoMapper.selectStorageCountByTypeAndCodeOrName(drugType,value);
        if(count==0){
            return page;
        }
        int start = (currentPage-1)*pageSize;
        List<StorageInfo> list = iStorageInfoMapper.selectStorageByTypeAndCodeOrName(drugType,value,start,pageSize);
        page.setTotal(count);
        page.setRecords(list);
        return page;

    }

    @Override
    public int insertStorageInfo(StorageInfo storageInfo) {
       return  iStorageInfoMapper.insert(storageInfo);



    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int updateStorageById(StorageInfo storageInfo) {

        return  iStorageInfoMapper.updateById(storageInfo);
    }

    @Override
    public int deleteStorageById(StorageInfo storageInfo) {
        return iStorageInfoMapper.deleteById(storageInfo.getId());
    }

    @Override
    public StorageInfo getStorageByDrugCode(String drugCode) {
        LambdaQueryWrapper<StorageInfo> queryWrapper =new QueryWrapper().lambda();
        queryWrapper.eq(StorageInfo::getDrugCode,drugCode);
        StorageInfo storageInfo = iStorageInfoMapper.selectOne(queryWrapper);
        return storageInfo;
    }


}
